import qs from 'qs'

export default {
  data () {
    return {
      permission_add: false,
      permission_modify: false,
      permission_delete: false,
      permission_import: false,
      permission_export: false,
      ${(businessCode?uncap_first)!''}SearchFormShowFull: false,
      ${(businessCode?uncap_first)!''}SearchForm: {
        ${(businessCode?uncap_first)!''}Name: '',
        ${(businessCode?uncap_first)!''}Code: '',
        createdDate: [],
        pageSize: 20,
        pageNumber: 1,
        fuzzySearch: true
      },
      ${(businessCode?uncap_first)!''}SearchFormCreatedDateOptions: {
        shortcuts: [{
          text: '最近7天',
          onClick (picker) {
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, new Date()])
          }
        }, {
          text: '最近30天',
          onClick (picker) {
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, new Date()])
          }
        }, {
          text: '最近90天',
          onClick (picker) {
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, new Date()])
          }
        }]
      },
      ${(businessCode?uncap_first)!''}SearchPageLoading: false,
      toolbarDeletes${businessCode!''}Loading: false,
      toolbarImport${businessCode!''}Loading: false,
      ${(businessCode?uncap_first)!''}SearchPageDeleteLoading: false,
      ${(businessCode?uncap_first)!''}SearchPage: {
        datas: [],
        totalElements: 0,
        totalPages: 0,
        hasPrevious: true,
        previous: 1,
        hasNext: true,
        next: 2,
        pages: [
          1,
          2,
          3,
          4,
          5,
          6
        ],
        page: 0,
        pageSize: 25
      },
      ${(businessCode?uncap_first)!''}SearchPageSelection: [],
      ${(businessCode?uncap_first)!''}AddFormSubmitLoading: false,
      ${(businessCode?uncap_first)!''}AddFormDialogVisible: false,
      ${(businessCode?uncap_first)!''}AddForm: {
        ${(businessCode?uncap_first)!''}Code: '',
        name: ''
      },
      ${(businessCode?uncap_first)!''}AddFormRules: {
        ${(businessCode?uncap_first)!''}Code: [
          {required: true, message: `请输入${businessName!}编号`, trigger: 'blur'},
          {min: 1, max: 36, message: '${businessName!}编号长度在 1 到 36 个字符', trigger: 'blur'}
        ],
        name: [
          {required: true, message: `请输入${businessName!}名称`, trigger: 'blur'},
          {min: 1, max: 36, message: '${businessName!}名称长度在 1 到 36 个字符', trigger: 'blur'}
        ]
      },
      ${(businessCode?uncap_first)!''}ModifyFormDialogVisible: false,
      ${(businessCode?uncap_first)!''}ModifyFormSubmitLoading: false,
      ${(businessCode?uncap_first)!''}ModifyForm: {
        ${(businessCode?uncap_first)!''}Code: '',
        name: ''
      },
      ${(businessCode?uncap_first)!''}ModifyFormRules: {
        ${(businessCode?uncap_first)!''}Code: [
          {required: true, message: `请输入${businessName!}编号`, trigger: 'blur'},
          {min: 1, max: 36, message: '${businessName!}编号长度在 1 到 36 个字符', trigger: 'blur'}
        ],
        name: [
          {required: true, message: `请输入${businessName!}名称`, trigger: 'blur'},
          {min: 1, max: 36, message: '${businessName!}名称长度在 1 到 36 个字符', trigger: 'blur'}
        ]
      },
      ${(businessCode?uncap_first)!''}ViewFormDialogVisible: false,
      ${(businessCode?uncap_first)!''}ViewForm: {
        ${(businessCode?uncap_first)!''}Code: '',
        name: ''
      }
    }
  },
  computed: {
    ${(businessCode?uncap_first)!''}SearchFormQueryString () {
      let query = {}

      for (let key in this.${(businessCode?uncap_first)!''}SearchForm) {
        query[key] = this.${(businessCode?uncap_first)!''}SearchForm[key]
      }

      query.pageSize = query.pageSize * 1000

      return qs.stringify(query, {arrayFormat: 'repeat'})
    }
  },
  created () {
    this.$http({
      url: '${api_root_url!}/v1/${moduleMapperCode}/${(businessCode?uncap_first)!''}/index.json',
      method: 'post'
    }).then(response => {
      for (let key in response.data) {
        this[key] = response.data[key]
      }
    })
  },
  methods: {
    ${(businessCode?uncap_first)!''}SearchFormSubmit (pageNumber) {
      if (pageNumber) {
        this.${(businessCode?uncap_first)!''}SearchForm.pageNumber = pageNumber
      }

      this.$http({
        url: '${api_root_url!}/v1/${moduleMapperCode}/${(businessCode?uncap_first)!''}/r0001.json',
        method: 'post',
        data: this.${(businessCode?uncap_first)!''}SearchForm,
        ref: this,
        thenRequest () {
          this.ref.${(businessCode?uncap_first)!''}SearchPageLoading = true
        },
        thenResponse () {
          this.ref.${(businessCode?uncap_first)!''}SearchPageLoading = false
        }
      }).then(response => {
        this.${(businessCode?uncap_first)!''}SearchPage = response.data

        this.${(businessCode?uncap_first)!''}SearchPageSelection = []
      })
    },
    ${(businessCode?uncap_first)!''}SearchFormResetFields () {
      this.$refs.${(businessCode?uncap_first)!''}SearchForm.resetFields()
    },
    ${(businessCode?uncap_first)!''}SearchFormPaginationSizeChange (size) {
      this.${(businessCode?uncap_first)!''}SearchForm.pageSize = size

      this.${(businessCode?uncap_first)!''}SearchFormSubmit()
    },
    ${(businessCode?uncap_first)!''}SearchForm${businessCode!''}QuerySearchAsync (queryString, cb) {
      this.$http({
        url: '${api_root_url!}/v1/${moduleMapperCode}/${(businessCode?uncap_first)!''}/r0002.json',
        method: 'post',
        data: {term: queryString}
      }).then(response => {
        cb(response.data)
      })
    },
    toolbarDeletes${businessCode!''}Click () {
      if (!this.${(businessCode?uncap_first)!''}SearchPageSelection || this.${(businessCode?uncap_first)!''}SearchPageSelection.length < 1) {
        this.$message({
          type: 'warning',
          message: '未勾选${businessName!}数据'
        })

        return
      }

      this.$confirm('将删除${businessName!}数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let ${(businessCode?uncap_first)!''}Ids = this.${(businessCode?uncap_first)!''}SearchPageSelection.map(function (row, index, array) {
          return row.${(businessCode?uncap_first)!''}Id
        }).join(',')

        this.$http({
          url: '${api_root_url!}/v1/${moduleMapperCode}/${(businessCode?uncap_first)!''}/d0001.json',
          method: 'post',
          data: {${(businessCode?uncap_first)!''}Ids: ${(businessCode?uncap_first)!''}Ids},
          ref: this,
          thenRequest () {
            this.ref.toolbarDeletes${businessCode!''}Loading = true
          },
          thenResponse () {
            this.ref.toolbarDeletes${businessCode!''}Loading = false
          }
        }).then(response => {
          this.$message({
            type: 'success',
            message: '删除${businessName!}数据成功'
          })

          this.${(businessCode?uncap_first)!''}SearchFormSubmit()
        })
      }).catch(() => {
        this.$message({
          type: 'warning',
          message: '已取消删除${businessName!}数据'
        })
      })
    },
    toolbarImport${businessCode!''}BeforeUpload (file) {
      this.toolbarImport${businessCode!''}Loading = true

      return true
    },
    toolbarImport${businessCode!''}Success (data, file, fileList) {
      this.$refs.toolbarImport${businessCode!''}.clearFiles()

      this.toolbarImport${businessCode!''}Loading = false

      if (data.errorCode) {
        this.$message({
          type: 'error',
          message: data.error
        })

        return
      }

      this.$message({
        type: 'success',
        message: '导入${businessName!}数据成功'
      })
    },
    toolbarImport${businessCode!''}Error (err, file, fileList) {
      this.$message({
        type: 'error',
        message: err.message
      })

      this.$refs.toolbarImport${businessCode!''}.clearFiles()

      this.toolbarImport${businessCode!''}Loading = false
    },
    ${(businessCode?uncap_first)!''}SearchPageSelectionChange (val) {
      this.${(businessCode?uncap_first)!''}SearchPageSelection = val
    },
    ${(businessCode?uncap_first)!''}SearchPageViewClick (row) {
      this.$http({
        url: '${api_root_url!}/v1/${moduleMapperCode}/${(businessCode?uncap_first)!''}/view.json',
        method: 'post',
        data: {${(businessCode?uncap_first)!''}Id: row.${(businessCode?uncap_first)!''}Id}
      }).then(response => {
        this.${(businessCode?uncap_first)!''}ViewForm = response.data

        this.${(businessCode?uncap_first)!''}ViewFormDialogVisible = true
      })
    },
    ${(businessCode?uncap_first)!''}SearchPageModifyClick (row) {
      this.$http({
        url: '${api_root_url!}/v1/${moduleMapperCode}/${(businessCode?uncap_first)!''}/modify.json',
        method: 'post',
        data: {${(businessCode?uncap_first)!''}Id: row.${(businessCode?uncap_first)!''}Id}
      }).then(response => {
        this.${(businessCode?uncap_first)!''}ModifyForm = response.data

        this.${(businessCode?uncap_first)!''}ModifyFormDialogVisible = true
      })
    },
    ${(businessCode?uncap_first)!''}SearchPageDeleteClick (row) {
      this.$confirm('将删除${businessName!}数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: '${api_root_url!}/v1/${moduleMapperCode}/${(businessCode?uncap_first)!''}/d0001.json',
          method: 'post',
          data: {${(businessCode?uncap_first)!''}Id: row.${(businessCode?uncap_first)!''}Id},
          ref: this,
          thenRequest () {
            this.ref.${(businessCode?uncap_first)!''}SearchPageDeleteLoading = true
          },
          thenResponse () {
            this.ref.${(businessCode?uncap_first)!''}SearchPageDeleteLoading = false
          }
        }).then(response => {
          this.$message({
            type: 'success',
            message: '删除${businessName!}数据成功'
          })

          this.${(businessCode?uncap_first)!''}SearchFormSubmit()
        })
      }).catch(() => {
        this.$message({
          type: 'warning',
          message: '已取消删除${businessName!}数据'
        })
      })
    },
    ${(businessCode?uncap_first)!''}AddFormDialogClosed () {
      this.$refs.${(businessCode?uncap_first)!''}AddForm.resetFields()
    },
    ${(businessCode?uncap_first)!''}AddFormSubmit () {
      this.$refs.${(businessCode?uncap_first)!''}AddForm.validate((valid) => {
        if (!valid) {
          return
        }

        this.$http({
          url: '${api_root_url!}/v1/${moduleMapperCode}/${(businessCode?uncap_first)!''}/c0001.json',
          method: 'post',
          data: this.${(businessCode?uncap_first)!''}AddForm,
          ref: this,
          thenRequest () {
            this.ref.${(businessCode?uncap_first)!''}AddFormSubmitLoading = true
          },
          thenResponse () {
            this.ref.${(businessCode?uncap_first)!''}AddFormSubmitLoading = false
          }
        }).then(response => {
          this.$message({
            type: 'success',
            message: '${businessName!}新增成功'
          })

          this.${(businessCode?uncap_first)!''}SearchFormSubmit()

          this.${(businessCode?uncap_first)!''}AddFormDialogVisible = false
        })
      })
    },
    ${(businessCode?uncap_first)!''}ViewFormDialogClosed () {
      this.$refs.${(businessCode?uncap_first)!''}ViewForm.resetFields()
    },
    ${(businessCode?uncap_first)!''}ModifyFormDialogClosed () {
      this.$refs.${(businessCode?uncap_first)!''}ModifyForm.resetFields()
    },
    ${(businessCode?uncap_first)!''}ModifyFormSubmit () {
      this.$refs.${(businessCode?uncap_first)!''}ModifyForm.validate((valid) => {
        if (!valid) {
          return
        }

        this.$http({
          url: '${api_root_url!}/v1/${moduleMapperCode}/${(businessCode?uncap_first)!''}/u0001.json',
          method: 'post',
          data: this.${(businessCode?uncap_first)!''}ModifyForm,
          ref: this,
          thenRequest () {
            this.ref.${(businessCode?uncap_first)!''}ModifyFormSubmitLoading = true
          },
          thenResponse () {
            this.ref.${(businessCode?uncap_first)!''}ModifyFormSubmitLoading = false
          }
        }).then(response => {
          this.$message({
            type: 'success',
            message: '${businessName!}修改成功'
          })

          this.${(businessCode?uncap_first)!''}SearchFormSubmit()

          this.${(businessCode?uncap_first)!''}ModifyFormDialogVisible = false
        })
      })
    }
  }
}